$(function(){
    obj.init();
});

var obj = new function() {
    'use strict';
    return {
        dataGrid: null,
        init: function () {
            this.dataGrid = new xhDataGrid($('#List'), 'Read/getSensitive');
            this.dataGrid.parseToolbar().init().activeBtn($(), $('#bt_list_del'));

            this.listener();
        },
        listener: function () {
            var $this = this;
            xhTool.bindDocKeyDown(
                $('#bt_list_search').click(function () {
                    $this.funObj().search();
                })
            );
            //增加
            $('#bt_list_add').click(function () {
                $this.funObj().addSensitive();
            });
            //删除
            $('#bt_list_del').click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().delSensitive();
            });
        },
        funObj: function () {
            var $this = this;
            return {
                search: function () {
                    $this.dataGrid.reload({
                        keyword: xhExtra.getValue($('#list_keyword'))
                    });
                },
                addSensitive: function () {
                    window.top.swal({
                        title: '添加敏感词',
                        input: 'text',
                        inputPlaceholder: '";"分割',
                        showCancelButton: true,
                        cancelButtonText: '取消',
                        confirmButtonText: '添加',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        preConfirm: function(words) {
                            return new Promise(function (resolve, reject) {
                                if (!words) {
                                    reject('敏感词为空！');
                                } else {
                                    xhExtra.ajaxRequest('Read/addSensitive', {words: words.split(';')}, {check: true, success: true, table: $this.dataGrid});
                                }
                            })
                        }
                    });
                },
                delSensitive: function () {
                    xhExtra.askChangeStatus('Read/delSensitive', {ids: $this.dataGrid.getSelectValues('id')}, obj.dataGrid, '确认删除？');
                }
            }
        }
    }
};